/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package filesecurity;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.KeySpec;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.SecretKeySpec;

/**
 *
 * @author lehoangkhanh
 */
public class KeyFac {
    public static SecretKey secretKeyGen(int algo){
        try {
            KeyGenerator keyGen = KeyGenerator.getInstance(Crypto.getAlgo(algo));
            if(Crypto.getAlgo(algo) == "DES"){
                keyGen.init(56);
            }
            if(Crypto.getAlgo(algo) == "AES"){
                keyGen.init(128);
            }
            SecretKey secretKey = keyGen.generateKey();
            return secretKey;
        } catch (NoSuchAlgorithmException ex) {
            Logger.getLogger(KeyFac.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }
    public static SecretKey sKeyGenWithPass(String pass){
        SecretKeyFactory factory = null;
        try {
            factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
        } catch (NoSuchAlgorithmException ex) {
            Logger.getLogger(Utils.class.getName()).log(Level.SEVERE, null, ex);
        }
        KeySpec spec = new PBEKeySpec(pass.toCharArray(),"notcare".getBytes(), 65536, 128);
        SecretKey tmp = null;
        
        try {
            tmp = factory.generateSecret(spec);
        } catch (InvalidKeySpecException ex) {
            Logger.getLogger(Utils.class.getName()).log(Level.SEVERE, null, ex);
        }
        SecretKey secretKey = new SecretKeySpec(tmp.getEncoded(), "AES");
        return secretKey; 
    }
    public static KeyPair keyPairGen(int keylength){
        KeyPairGenerator kpg = null;
        try {
            kpg = KeyPairGenerator.getInstance("RSA");
        } catch (NoSuchAlgorithmException ex) {
            Logger.getLogger(KeyFac.class.getName()).log(Level.SEVERE, null, ex);
        } 
        kpg.initialize(keylength);
        KeyPair kp = kpg.genKeyPair();
        return kp;
    }
    public static PublicKey getPublicKey(KeyPair keypair){
        return keypair.getPublic();
    }
    public static PrivateKey getPrivateKey(KeyPair keypair){
        return keypair.getPrivate();
    }
    public static void exportKeyPair(User userinfo, String filepath){
        try{
            File fileOne = new File(filepath);
            FileOutputStream fos = new FileOutputStream(fileOne);
            ObjectOutputStream oos=new ObjectOutputStream(fos);

            oos.writeObject(userinfo);
            oos.flush();
            oos.close();
            fos.close();
        } catch (FileNotFoundException ex) {
            Logger.getLogger(KeyFac.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(KeyFac.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    public static User importKeyPair(String filepath){
        User userinfo = new User();
        try{
            File toRead = new File(filepath);
            FileInputStream fis = new FileInputStream(toRead);
            ObjectInputStream ois = new ObjectInputStream(fis);

            userinfo = (User)ois.readObject();

            ois.close();
            fis.close();
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Utils.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Utils.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Utils.class.getName()).log(Level.SEVERE, null, ex);
        }
        return userinfo;
    }
}
